<?php

namespace app\admin\controller\auth;

use app\common\controller\BaseAdminController;
use app\common\model\AuthRule;

class Rule extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new AuthRule();
        $allList = $this->model->where('ismenu',1)
            ->order('id asc,weigh desc')
            ->select()->toArray();
        if(!empty($allList)){
            foreach ($allList as &$v){
                $v['title'] = __($v['title']);
            }
            unset($v);
            $allList = list_to_tree($allList,'id','pid','children');
            $allList = array_merge([[
                'id'=>0,
                'title'=>'顶级菜单',
            ]],$allList);
        }
        $this->assignconfig('allList',$allList);
        $addons = get_addons_list();
        if(empty($addons)){
            $newaddons = [];
            foreach ($addons as $k=>$v){
                $tem = get_addons_info($v);
                if(!empty($tem)){
                    $newaddons[] = [
                        'title'=>$tem['title'],
                        'value'=>'addons/'.$v
                    ];
                }
            }
            $addons = $newaddons;
        }
        $apps = get_app_list();
        if(!empty($apps)){
            $newapps = [];
            foreach ($apps as $k=>$v){
                $newapps[] = [
                    'title'=>ucfirst($v),
                    'value'=>$v
                ];
            }
            $apps = $newapps;
        }
        $allAddons = array_merge($addons,$apps);
        $this->assignconfig('allAddons',$allAddons);

    }
    public function index()
    {
        if($this->request->isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildParameter();
            $is_tree = $this->request->param('isTree',0);
            if($is_tree){
                $list = $this->model->where($where)
                    ->order('id asc,weigh desc')
//                    ->order($sort, $order)
                    ->select()->toArray();
                foreach ($list as &$v){
                    $v['title'] = __($v['title']);
                }
                unset($v);
                $pk = $this->model->getPk();
                $pid_field = $this->request->param('pid_field','pid');
                if(empty($pid_field)){
                    $pid_field = 'pid';
                }
                if(!empty($list)){
                    $min = min(array_column($list,$pid_field));
                    $list = list_to_tree($list,$pk,$pid_field,'children',$min);
                }
                if($this->index_get_data){
                    return [
                        'total' => count($list),
                        'rows' => $list,
                    ];
                }
                return $this->success("获取成功", [
                    'total' => count($list),
                    'rows' => $list,
                ]);
            }else{
                $list = $this->model->where($where)
                    ->order('id asc,weigh desc')
                    ->paginate($limit);
            }
            if($this->index_get_data){
                return [
                    'total' => $list->total(),
                    'rows' => $list->items(),
                ];
            }
            return $this->success("获取成功", [
                'total' => $list->total(),
                'rows' => $list->items(),
            ]);
        }
        return $this->view->fetch();
    }
    public function edit($ids=null)
    {
        if(empty($ids)){
            $ids = $this->request->param('ids');
        }
        $row = $this->model->field($this->editField)->find($ids);
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            $res = $row->save($data);
            if($res){
                return $this->success("修改成功");
            }else{
                return $this->error("修改失败");
            }
        }
        $row->title = __($row->title);
        $this->assignconfig('row',$row);
        return $this->view->fetch();
    }
}